//
//  EntryViewController.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/1.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "EntryViewController.h"
#import "AnswersTableViewCell.h"
#import "CanHelpPeopleCollectionViewCell.h"
#import "ImagePlayerView.h"
#import "JobDetailVC.h"
#import "ManageJobIntensionVC.h"
#import "JobListVC.h"
#import "WorkmateMainVC.h"
#import "SearchViewController.h"
#import "EntryProtectDetailVC.h"
#import "IncumbentModel.h"
#import "IntentionalModel.h"
#import "PositionModel.h"
#import "DecryptModel.h"
#import "QuestionModel.h"
#import "JobSeekerMainViewController.h"
#import "NSUDManager.h"
#import "QuestionAnwserModel.h"
#import "PlayVoice.h"
#import "HelpDetailVC.h"
#import "AdvertModel.h"
#import "AdvertWebViewController.h"
#import "BHInfiniteScrollView.h"
#import "JobCertificationViewController.h"

@interface EntryViewController ()<UITableViewDelegate,UITableViewDataSource,BHInfiniteScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *statusHeight;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrData;
@property (strong, nonatomic) IBOutlet UIView *headView;
#pragma mark ------广告位
@property (nonatomic, strong) NSMutableArray *bannerArray;
@property (nonatomic, strong) NSMutableArray *imageURLs;
@property (weak, nonatomic) IBOutlet UIView *bannerView;

#pragma mark ------综合匹配
@property (weak, nonatomic) IBOutlet UIView *selectJobView1;
@property (weak, nonatomic) IBOutlet UILabel *jobNameLab1;
@property (weak, nonatomic) IBOutlet UILabel *selectJobNameLab1;
@property (weak, nonatomic) IBOutlet UIView *jobView1;
@property (weak, nonatomic) IBOutlet UIButton *jobBtn1;

@property (weak, nonatomic) IBOutlet UIView *selectJobView2;
@property (weak, nonatomic) IBOutlet UILabel *jobNameLab2;
@property (weak, nonatomic) IBOutlet UILabel *selectJobNameLab2;
@property (weak, nonatomic) IBOutlet UIView *jobView2;
@property (weak, nonatomic) IBOutlet UIButton *jobBtn2;

@property (weak, nonatomic) IBOutlet UIView *selectJobView3;
@property (weak, nonatomic) IBOutlet UILabel *jobNameLab3;
@property (weak, nonatomic) IBOutlet UILabel *selectJobNameLab3;
@property (weak, nonatomic) IBOutlet UIView *jobView3;
@property (weak, nonatomic) IBOutlet UIButton *jobBtn3;

@property (nonatomic, strong) NSMutableArray *intensionArray;
@property (nonatomic, strong) NSMutableArray *positionArray;
@property (nonatomic, assign) NSInteger positionCount;

#pragma mark ------谁能帮我
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic)  NSMutableArray *helpArr;//谁能帮我数组

#pragma mark - 职位解密
@property (nonatomic, strong) NSMutableArray *decryptArray;

#pragma mark - 问答列表
@property (nonatomic, strong) NSMutableArray *questionArray;

@end

@implementation EntryViewController {
    NSInteger _selectIntension;
    
    NSInteger _questionPageNo;
}


//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//}
//-(void)viewDidAppear:(BOOL)animated{
//    [super viewDidAppear:animated];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//}
//-(void)viewDidDisappear:(BOOL)animated{
//    [super viewDidDisappear:animated];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"首页";
    CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
    self.statusHeight.constant = statusRect.size.height;
    self.navigationController.navigationBarHidden = YES;
    
    NSString *version = [UIDevice currentDevice].systemVersion;
    if (version.doubleValue >= 11.0) {
        self.searchViewTopHeight.constant = 7;
    } else {
        self.searchViewTopHeight.constant = 27;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadIntentionPositionData) name:SHIRefreshIntentionalList object:nil];
    
    [self setupTableView];
    
    [self setupCollectionView];
}
/* 加载广告 */
-(void)loadAdView{
    WS(weakSelf)
    [SendRequest getAdvertisementsWithresult:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"查询首页广告列表 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [AdvertModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"] ];
            [weakSelf.bannerArray removeAllObjects];
            [weakSelf.bannerArray addObjectsFromArray:datas];
            
            [weakSelf.imageURLs removeAllObjects];
            for (AdvertModel *advert in weakSelf.bannerArray) {
                NSString *imgUrl = ImageURLStringWithKey(advert.advertisementImg);
                [weakSelf.imageURLs addObject:imgUrl];
            }
            [weakSelf resetBannerView];
            
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetBannerView {
    for (UIView *con in self.bannerView.subviews) {
        [con removeFromSuperview];
    }
    
    BHInfiniteScrollView *bannerView = [[BHInfiniteScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.bannerView.mj_h)];
    bannerView.imagesArray = self.imageURLs;
    bannerView.delegate = self;
    [self.bannerView addSubview:bannerView];
}

- (void)infiniteScrollView:(BHInfiniteScrollView *)infiniteScrollView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"banner--index=%zd", index);
    AdvertModel *model = [self.bannerArray objectAtIndex:index];
    AdvertWebViewController *web = [[AdvertWebViewController alloc] init];
    web.advertModel = model;
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
}

/* 加载列表 */
-(void)setupTableView{
    self.headView.frame = CGRectMake(0, 0, kScreenWidth, 909);
    self.tableView.tableHeaderView = self.headView;
    [self.tableView registerNib:[UINib nibWithNibName:@"AnswersTableViewCell" bundle:nil] forCellReuseIdentifier:@"WorkmateAnswersTableViewCell"];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        
    }
    if (IOS11Later) {
        self.tableView.tableFooterView = [[UIView alloc]init];
        
    }else{
        self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    }
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _questionPageNo = 1;

        /** 广告 */
        [self loadAdView];
        
        /** 谁能帮我 */
        [self loadFindPeopleData];
        
        /** 综合匹配--职位列表 */
        [self loadIntentionPositionData];
        
        /** 职位解密 */
        [self getIncumenbentData];
        
        [self loadQuestionListData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _questionPageNo += 1;
        [self loadQuestionListData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

/* 谁能帮我 */
-(void)setupCollectionView{
    [self.collectionView registerNib:[UINib nibWithNibName:@"CanHelpPeopleCollectionViewCell" bundle:nil]  forCellWithReuseIdentifier:@"CanHelpPeopleCollectionViewCell"];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 15;
    flowLayout.minimumLineSpacing = 15;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsZero;
    flowLayout.itemSize = CGSizeMake(120,158);
    flowLayout.sectionInset = UIEdgeInsetsMake(0,15, 0, 15);
    self.collectionView.collectionViewLayout = flowLayout;
}

#pragma mark -- 加载职位解密数据
/** 职位解密 */
- (void)getIncumenbentData {
    WS(weakSelf)
    [SendRequest getIncumbentDecryptOfUserRelevanceWithPageNo:@"1" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"查询职位解密列表 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [DecryptModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            [weakSelf.decryptArray removeAllObjects];
            [weakSelf.decryptArray addObjectsFromArray:datas];
//            [weakSelf.decryptArray addObject:[datas objectAtIndex:0]];

            [weakSelf resetDecryptView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetDecryptView {
    for (UIView *con in self.decryptView.subviews) {
        [con removeFromSuperview];
    }
    
    // 上下间距是10，一条数据的高度是22
    CGFloat decryptH = 86;
    if (self.decryptArray.count == 0) {
        decryptH = 0;
    } else if (self.decryptArray.count >= 3) {
        decryptH = 86;
    } else {
        decryptH = 20 + 22 * self.decryptArray.count;
    }
    
    self.decryptViewHeight.constant = decryptH;
    self.headView.frame = CGRectMake(0, 0, kScreenWidth, 909 - 86 + decryptH);
    self.tableView.tableHeaderView = self.headView;
    [self.tableView reloadData];
    
    UIView *indicatorV = [[UIView alloc] initWithFrame:CGRectMake(0, decryptH * 0.25, 4, decryptH * 0.5)];
    indicatorV.backgroundColor = UIColorFromRGB(0x00BCCC);
    [self.decryptView addSubview:indicatorV];
    
    for (NSInteger i = 0; i < self.decryptArray.count; i++) {
        if (i > 2) {  // 最多显示三个
            break;
        }
        
        DecryptModel *model = [self.decryptArray objectAtIndex:i];
        
        UIView *leftV = [[UIView alloc] initWithFrame:CGRectMake(19, 10 + 9.5 + 22 * i, 3, 3)];
        leftV.backgroundColor = indicatorV.backgroundColor;
        [self.decryptView addSubview:leftV];
        
        UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(27, 10 + 22 * i, kScreenWidth - 120, 22)];
        titleL.text = model.title;
        titleL.font = [UIFont fontWithName:@"PingFang SC" size:14];
        titleL.textColor = UIColorFromRGB(0x383F42);
        [self.decryptView addSubview:titleL];
        
        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleL.frame) + 18, 10 + 22 * i + 4.5, 13, 13)];
        iv.image = [UIImage imageNamed:@"ic_lock"];
        [self.decryptView addSubview:iv];
        
        UILabel *numL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iv.frame) + 7, 10 + 22 * i, 63 - 38, 22)];
        numL.text = model.quantityPurchased;
        numL.textColor = UIColorFromRGB(0x96ABB5);
        numL.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11];
        [self.decryptView addSubview:numL];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 10 + 22 * i, kScreenWidth - 30, 22)];
        btn.tag = i;
        [btn addTarget:self action:@selector(decryptBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.decryptView addSubview:btn];
    }
}

- (void)decryptBtnClick:(UIButton *)btn {
    EntryProtectDetailVC *vc = [[EntryProtectDetailVC alloc] init];
    vc.index = 1;
    DecryptModel *decrypt = [self.decryptArray objectAtIndex:btn.tag];
    vc.decryptId = decrypt.incumbentDecryptId;
    
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

/** 加载列表数据 */
- (void)loadQuestionListData {
    WS(weakSelf)
    [SendRequest getQuestiosOfUserRelevanceWithPageNo:[NSString stringWithFormat:@"%zd", _questionPageNo] result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"首页问答 = %@", [result objectForKey:@"data"]);
            
//            NSArray *datas = [QuestionModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            NSMutableArray *datas = [NSMutableArray array];
            NSArray *tempArr = [[result objectForKey:@"data"] objectForKey:@"list"];
            for (NSInteger i = 0; i< tempArr.count; i++) {
                QuestionModel *models = [QuestionModel mj_objectWithKeyValues:tempArr[i]];
                NSArray *anwsers = tempArr[i][@"anwsers"];
                if (anwsers.count > 0) {
                    QuestionAnwserModel *qaModel = [QuestionAnwserModel mj_objectWithKeyValues:anwsers[0]];
                    models.anwserModel = qaModel;
                }
                [datas addObject: models];
            }
            if (_questionPageNo == 1) {
                [weakSelf.questionArray removeAllObjects];
                [weakSelf.questionArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
            } else {
                [weakSelf.questionArray addObjectsFromArray:datas];
                [weakSelf.tableView reloadData];
                if (datas.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

#pragma mark - ImagePlayerViewDelegate 广告代理
- (NSInteger)numberOfItems
{
    return self.imageURLs.count;
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index
{
    // recommend to use SDWebImage lib to load web image
    NSString *imgUrl = [self.imageURLs objectAtIndex:index];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:IMAGE(@"banner默认图")];
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView didTapAtIndex:(NSInteger)index
{
    NSLog(@"点击了广告图----%zd", index);
//    JobSeekerMainViewController *jobSeeker = [[JobSeekerMainViewController alloc] init];
    
    AdvertModel *model = [self.bannerArray objectAtIndex:index];
    AdvertWebViewController *web = [[AdvertWebViewController alloc] init];
    web.advertModel = model;
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark ---- UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.helpArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"CanHelpPeopleCollectionViewCell";
    CanHelpPeopleCollectionViewCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
    cell.contentView.backgroundColor = UIColorFromRGB(0xF2F5FA);
    cell.model = [self.helpArr objectAtIndex:indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WorkmateMainVC *wmVC = [[WorkmateMainVC alloc] init];
    IncumbentModel *model = [self.helpArr objectAtIndex:indexPath.row];
    wmVC.userId = model.userId;
    wmVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:wmVC animated:YES];
}

#pragma mark ------tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.questionArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"WorkmateAnswersTableViewCell";
    AnswersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    QuestionModel *model = self.questionArray[indexPath.row];
    cell.model = model;
    cell.audioButton.tag = indexPath.row;
    cell.wordButton.tag = indexPath.row;
    [cell.audioButton addTarget:self action:@selector(voiceButonClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.wordButton addTarget:self action:@selector(voiceButonClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)voiceButonClick:(UIButton *)sender{
    QuestionModel *questionModel = self.questionArray[sender.tag];
    UserModel *user = [NSUDManager currentUser];
    QuestionAnwserModel *model =  questionModel.anwserModel;
    if (model) {
        if (model.isPurchased || [user.userId isEqualToString:[NSString stringWithFormat:@"%zd",model.publisherId]]||[user.userId isEqualToString:questionModel.publisherId]) {
            if ([model.anwserType isEqualToString:@"ANWSER_TYPE_SOUND"]) {
                /**语音*/
                [PlayVoice audioPlayWithUrl:model.soundKey];
            }else  if ([model.anwserType isEqualToString:@"ANWSER_TYPE_WORDS"]) {
                /**文字*/
                
            }
            
        }else{
            /**偷听*/
            [self showProgressWithView:self.view animated:YES];
            __weak typeof(self) weakSelf = self;
            [ [CommonUtils sharedCommonUtils] payWithType:@"GOODS_TYPE_TT" relatedId:[NSString stringWithFormat:@"%zd",model.anwserId] result:^(NSDictionary *result, BOOL paySuccess) {
                [self hideProgress:self.view animated:YES];
                if (paySuccess) {
                    
                    [weakSelf.tableView.mj_header beginRefreshing];
                    [PlayVoice audioPlayWithUrl:model.soundKey];
                }else{
//                    [CommonUtils showToast:[result objectForKey:@"msg"]];
                }
            }];
        }
    }
    
}
- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    QuestionModel *questionModel = self.questionArray[indexPath.row];
    HelpDetailVC *vc = [[HelpDetailVC alloc]init];
    vc.relatedId = [NSString stringWithFormat:@"%@",questionModel.questionId];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ------综合匹配
/** 查询意向职位列表 */
- (void)loadIntentionPositionData {
    WS(weakSelf)
    [SendRequest getIntentionalPositionWithResult:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"查询意向职位列表 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [IntentionalModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
            [weakSelf.intensionArray removeAllObjects];
            [weakSelf.intensionArray addObjectsFromArray:datas];
            
            [weakSelf resetIntensionPosition];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetIntensionPosition {
    if (self.intensionArray.count == 0) {
        self.jobView1.hidden = YES;
        self.selectJobView1.hidden = YES;
        self.jobBtn1.hidden = YES;
        self.jobView2.hidden = YES;
        self.selectJobView2.hidden = YES;
        self.jobBtn2.hidden = YES;
        self.jobView3.hidden = YES;
        self.selectJobView3.hidden = YES;
        self.jobBtn3.hidden = YES;
        _selectIntension = 0;
    } else if (self.intensionArray.count == 1) {
        self.jobView1.hidden = NO;
        self.selectJobView1.hidden = NO;
        self.jobBtn1.hidden = NO;
        IntentionalModel *model1 = [self.intensionArray objectAtIndex:0];
        self.jobNameLab1.text = model1.jobName;
        self.selectJobNameLab1.text = model1.jobName;
        self.jobView2.hidden = YES;
        self.selectJobView2.hidden = YES;
        self.jobBtn2.hidden = YES;
        self.jobView3.hidden = YES;
        self.selectJobView3.hidden = YES;
        self.jobBtn3.hidden = YES;
        _selectIntension = 1;
    } else if (self.intensionArray.count == 2) {
        self.jobView1.hidden = NO;
        self.selectJobView1.hidden = NO;
        self.jobBtn1.hidden = NO;
        IntentionalModel *model1 = [self.intensionArray objectAtIndex:0];
        self.jobNameLab1.text = model1.jobName;
        self.selectJobNameLab1.text = model1.jobName;
        self.jobView2.hidden = NO;
        self.selectJobView2.hidden = YES;
        self.jobBtn2.hidden = NO;
        IntentionalModel *model2 = [self.intensionArray objectAtIndex:1];
        self.jobNameLab2.text = model2.jobName;
        self.selectJobNameLab2.text = model2.jobName;
        self.jobView3.hidden = YES;
        self.selectJobView3.hidden = YES;
        self.jobBtn3.hidden = YES;
        _selectIntension = 1;
    } else {
        self.jobView1.hidden = NO;
        self.selectJobView1.hidden = NO;
        self.jobBtn1.hidden = NO;
        IntentionalModel *model1 = [self.intensionArray objectAtIndex:0];
        self.jobNameLab1.text = model1.jobName;
        self.selectJobNameLab1.text = model1.jobName;
        self.jobView2.hidden = NO;
        self.selectJobView2.hidden = YES;
        self.jobBtn2.hidden = NO;
        IntentionalModel *model2 = [self.intensionArray objectAtIndex:1];
        self.jobNameLab2.text = model2.jobName;
        self.selectJobNameLab2.text = model2.jobName;
        self.jobView3.hidden = NO;
        self.selectJobView3.hidden = YES;
        self.jobBtn3.hidden = NO;
        IntentionalModel *model3 = [self.intensionArray objectAtIndex:2];
        self.jobNameLab3.text = model3.jobName;
        self.selectJobNameLab3.text = model3.jobName;
        _selectIntension = 1;
    }
    
    if (self.intensionArray.count > 0) {
        IntentionalModel *model1 = [self.intensionArray objectAtIndex:0];
        [self getPostionListByIntentionalId:model1.intentionalId andFlag:NO];
    } else {
#warning 清除页面数据
        self.positionCountL.text = @"更多（共0条）";
        
        self.positionView1.hidden = YES;
        self.positionView2.hidden = YES;
        self.positionView3.hidden = YES;
        self.PositionNoDataBtn1.hidden = YES;
        self.PositionNoDataBtn2.hidden = NO;
        self.PositionNoDataBtn3.hidden = YES;
    }
}

- (IBAction)jobButtonClick:(UIButton *)sender {
    switch (sender.tag) {
        case 1:
        {
            self.selectJobView1.hidden = NO;
            self.selectJobView2.hidden = YES;
            self.selectJobView3.hidden = YES;
            
            if (_selectIntension != 1) {
                _selectIntension = 1;
                IntentionalModel *model = [self.intensionArray objectAtIndex:0];
                [self getPostionListByIntentionalId:model.intentionalId andFlag:YES];
            }
        }
            break;
        case 2:
        {
            self.selectJobView1.hidden = YES;
            self.selectJobView2.hidden = NO;
            self.selectJobView3.hidden = YES;
            if (_selectIntension != 2) {
                _selectIntension = 2;
                
                IntentionalModel *model = [self.intensionArray objectAtIndex:1];
                [self getPostionListByIntentionalId:model.intentionalId andFlag:YES];
            }
        }
            break;
        case 3:
        {
            self.selectJobView1.hidden = YES;
            self.selectJobView2.hidden = YES;
            self.selectJobView3.hidden = NO;
            if (_selectIntension != 3) {
                _selectIntension = 3;
                
                IntentionalModel *model = [self.intensionArray objectAtIndex:2];
                [self getPostionListByIntentionalId:model.intentionalId andFlag:YES];
            }
        }
            break;
        case 4:
        {
            ManageJobIntensionVC *mjiVC = [[ManageJobIntensionVC alloc] init];
            mjiVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:mjiVC animated:YES];
        }
        default:
            break;
    }
}

- (IBAction)jobDetailClick:(UIButton *)sender {
    JobDetailVC *vc = [[JobDetailVC alloc] init];
    PositionModel *position = [self.positionArray objectAtIndex:sender.tag];
    vc.positionId = position.positionId;
    
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getPostionListByIntentionalId:(NSString *)intentionId andFlag:(BOOL)flag {
    WS(weakSelf)
    if (flag) {
        [self showProgressWithView:self.view animated:YES];
    }
    [SendRequest getPostionListByIntentionalId:intentionId andPageNo:@"1" result:^(NSDictionary *result, NSError *error) {
        if (flag) {
            [weakSelf hideProgress:self.view animated:YES];
        }
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"根据意向职位ID查询职位列表 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [PositionModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            [weakSelf.positionArray removeAllObjects];
            [weakSelf.positionArray addObjectsFromArray:datas];
            weakSelf.positionCount = [[[result objectForKey:@"data"] objectForKey:@"totalCount"] integerValue];
            
            [weakSelf resetPositionsView];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetPositionsView {
    self.positionCountL.text = [NSString stringWithFormat:@"更多（共%zd条）", self.positionCount];
    
    if (self.positionArray.count == 0) {
        self.positionView1.hidden = YES;
        self.positionView2.hidden = YES;
        self.positionView3.hidden = YES;
        self.PositionNoDataBtn1.hidden = YES;
        self.PositionNoDataBtn2.hidden = NO;
        self.PositionNoDataBtn3.hidden = YES;
    } else if (self.positionArray.count == 1) {
        self.positionView1.hidden = NO;
        self.positionView2.hidden = YES;
        self.positionView3.hidden = YES;
        self.PositionNoDataBtn1.hidden = YES;
        self.PositionNoDataBtn2.hidden = NO;
        self.PositionNoDataBtn3.hidden = YES;
        PositionModel *position1 = [self.positionArray objectAtIndex:0];
        [self.positionHeaderIV1 sd_setImageWithURL:ImageURLWithKey(position1.companyLogo) placeholderImage:PlaceholderImage];
        if (position1.targetCompany) {
            self.positionTargetIV1.hidden = NO;
            self.positionScoreL1.hidden = YES;
        } else {
            self.positionTargetIV1.hidden = YES;
            self.positionScoreL1.hidden = NO;
            self.positionScoreL1.text = [NSString stringWithFormat:@"%@分", position1.avgScore];
        }
        self.positionPositionL1.text = position1.positionName;
        self.positionCompanyL1.text = position1.companyAbbreviatedName;
        self.positionAddressL1.text = [NSString stringWithFormat:@"%@-%@ | %@名认证员工点评", position1.regionName, position1.workingPlaceTradingArea, position1.incumbentCount];
        self.positionSalaryL1.text = [NSString stringWithFormat:@"%@k-%@k", position1.minSalary, position1.maxSalary];
        
    } else if (self.positionArray.count == 2) {
        self.positionView1.hidden = NO;
        self.positionView2.hidden = NO;
        self.positionView3.hidden = YES;
        self.PositionNoDataBtn1.hidden = YES;
        self.PositionNoDataBtn2.hidden = YES;
        self.PositionNoDataBtn3.hidden = NO;
        PositionModel *position1 = [self.positionArray objectAtIndex:0];
        [self.positionHeaderIV1 sd_setImageWithURL:ImageURLWithKey(position1.companyLogo) placeholderImage:PlaceholderImage];
        if (position1.targetCompany) {
            self.positionTargetIV1.hidden = NO;
            self.positionScoreL1.hidden = YES;
        } else {
            self.positionTargetIV1.hidden = YES;
            self.positionScoreL1.hidden = NO;
            self.positionScoreL1.text = [NSString stringWithFormat:@"%@分", position1.avgScore];
        }
        self.positionPositionL1.text = position1.positionName;
        self.positionCompanyL1.text = position1.companyAbbreviatedName;
        self.positionAddressL1.text = [NSString stringWithFormat:@"%@-%@ | %@名认证员工点评", position1.regionName, position1.workingPlaceTradingArea, position1.incumbentCount];
        self.positionSalaryL1.text = [NSString stringWithFormat:@"%@k-%@k", position1.minSalary, position1.maxSalary];
        
        PositionModel *position2 = [self.positionArray objectAtIndex:1];
        [self.positionHeaderIV2 sd_setImageWithURL:ImageURLWithKey(position2.companyLogo) placeholderImage:PlaceholderImage];
        if (position2.targetCompany) {
            self.positionTargetIV2.hidden = NO;
            self.positionScoreL2.hidden = YES;
        } else {
            self.positionTargetIV2.hidden = YES;
            self.positionScoreL2.hidden = NO;
            self.positionScoreL2.text = [NSString stringWithFormat:@"%@分", position2.avgScore];
        }
        self.positionPositionL2.text = position2.positionName;
        self.positionCompanyL2.text = position2.companyAbbreviatedName;
        self.positionAddressL2.text = [NSString stringWithFormat:@"%@-%@ | %@名认证员工点评", position2.regionName, position2.workingPlaceTradingArea, position2.incumbentCount];
        self.positionSalaryL2.text = [NSString stringWithFormat:@"%@k-%@k", position2.minSalary, position2.maxSalary];
    } else {
        self.positionView1.hidden = NO;
        self.positionView2.hidden = NO;
        self.positionView3.hidden = NO;
        self.PositionNoDataBtn1.hidden = YES;
        self.PositionNoDataBtn2.hidden = YES;
        self.PositionNoDataBtn3.hidden = YES;
        PositionModel *position1 = [self.positionArray objectAtIndex:0];
        [self.positionHeaderIV1 sd_setImageWithURL:ImageURLWithKey(position1.companyLogo) placeholderImage:PlaceholderImage];
        if (position1.targetCompany) {
            self.positionTargetIV1.hidden = NO;
            self.positionScoreL1.hidden = YES;
        } else {
            self.positionTargetIV1.hidden = YES;
            self.positionScoreL1.hidden = NO;
            self.positionScoreL1.text = [NSString stringWithFormat:@"%@分", position1.avgScore];
        }
        self.positionPositionL1.text = position1.positionName;
        self.positionCompanyL1.text = position1.companyAbbreviatedName;
        self.positionAddressL1.text = [NSString stringWithFormat:@"%@-%@ | %@名认证员工点评", position1.regionName, position1.workingPlaceTradingArea, position1.incumbentCount];
        self.positionSalaryL1.text = [NSString stringWithFormat:@"%@k-%@k", position1.minSalary, position1.maxSalary];
        
        PositionModel *position2 = [self.positionArray objectAtIndex:1];
        [self.positionHeaderIV2 sd_setImageWithURL:ImageURLWithKey(position2.companyLogo) placeholderImage:PlaceholderImage];
        if (position2.targetCompany) {
            self.positionTargetIV2.hidden = NO;
            self.positionScoreL2.hidden = YES;
        } else {
            self.positionTargetIV2.hidden = YES;
            self.positionScoreL2.hidden = NO;
            self.positionScoreL2.text = [NSString stringWithFormat:@"%@分", position2.avgScore];
        }
        self.positionPositionL2.text = position2.positionName;
        self.positionCompanyL2.text = position2.companyAbbreviatedName;
        self.positionAddressL2.text = [NSString stringWithFormat:@"%@-%@ | %@名认证员工点评", position2.regionName, position2.workingPlaceTradingArea, position2.incumbentCount];
        self.positionSalaryL2.text = [NSString stringWithFormat:@"%@k-%@k", position2.minSalary, position2.maxSalary];
        
        PositionModel *position3 = [self.positionArray objectAtIndex:2];
        [self.positionHeaderIV3 sd_setImageWithURL:ImageURLWithKey(position3.companyLogo) placeholderImage:PlaceholderImage];
        if (position3.targetCompany) {
            self.positionTargetIV3.hidden = NO;
            self.positionScoreL3.hidden = YES;
        } else {
            self.positionTargetIV3.hidden = YES;
            self.positionScoreL3.hidden = NO;
            self.positionScoreL3.text = [NSString stringWithFormat:@"%@分", position3.avgScore];
        }
        self.positionPositionL3.text = position3.positionName;
        self.positionCompanyL3.text = position3.companyAbbreviatedName;
        self.positionAddressL3.text = [NSString stringWithFormat:@"%@-%@ | %@名认证员工点评", position3.regionName, position3.workingPlaceTradingArea, position3.incumbentCount];
        self.positionSalaryL3.text = [NSString stringWithFormat:@"%@k-%@k", position3.minSalary, position3.maxSalary];
    }
}

#pragma mark - 更多综合匹配
- (IBAction)moreJobBtnClick:(id)sender {
    JobListVC *vc = [[JobListVC alloc] init];
    vc.intentionalArray = self.intensionArray;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
//    JobCertificationViewController *jobCVC = [[JobCertificationViewController alloc] init];
//    jobCVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:jobCVC animated:YES];
}

- (IBAction)decrypeBtnClick:(id)sender {
    EntryProtectDetailVC *vc = [[EntryProtectDetailVC alloc] init];
    vc.index = 1;
    if (sender == self.decryptBtn1) {
        DecryptModel *decrypt = [self.decryptArray objectAtIndex:0];
        vc.decryptId = decrypt.incumbentDecryptId;
    } else if (sender == self.decryptBtn2) {
        DecryptModel *decrypt = [self.decryptArray objectAtIndex:1];
        vc.decryptId = decrypt.incumbentDecryptId;
    } else {
        DecryptModel *decrypt = [self.decryptArray objectAtIndex:2];
        vc.decryptId = decrypt.incumbentDecryptId;
    }
    
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


- (IBAction)searchBtnClick:(id)sender {
    SearchViewController *search = [[SearchViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:search];
    [self presentViewController:nav animated:YES completion:nil];
}


/** 加载谁能帮我模块数据 */
- (void)loadFindPeopleData {
    WS(weakSelf)
//    [self showProgressWithView:self.view animated:YES];
    [SendRequest getMatchingIncumbentsWithPageNo:@"1" result:^(NSDictionary *result, NSError *error) {
//        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"谁能帮我 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            NSArray *dataIncument = [IncumbentModel mj_objectArrayWithKeyValuesArray:datas];
            
            [weakSelf.helpArr removeAllObjects];
            [weakSelf.helpArr addObjectsFromArray:dataIncument];
            
            [weakSelf.collectionView reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (NSMutableArray *)helpArr {
    if (!_helpArr) {
        _helpArr = [[NSMutableArray alloc] init];
    }
    return _helpArr;
}

- (NSMutableArray *)intensionArray {
    if (!_intensionArray) {
        _intensionArray = [[NSMutableArray alloc] init];
    }
    return _intensionArray;
}

- (NSMutableArray *)positionArray {
    if (!_positionArray) {
        _positionArray = [NSMutableArray array];
    }
    return _positionArray;
}

- (NSMutableArray *)decryptArray {
    if (!_decryptArray) {
        _decryptArray = [NSMutableArray array];
    }
    return _decryptArray;
}

- (NSMutableArray *)questionArray {
    if (!_questionArray) {
        _questionArray = [NSMutableArray array];
    }
    return _questionArray;
}

- (NSMutableArray *)bannerArray {
    if (!_bannerArray) {
        _bannerArray = [NSMutableArray array];
    }
    return _bannerArray;
}

- (NSMutableArray *)imageURLs {
    if (!_imageURLs) {
        _imageURLs = [NSMutableArray array];
    }
    return _imageURLs;
}

@end
